package com.qitmiaojie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qitmiaojie.controller.utils.Result;
import com.qitmiaojie.pojo.Material;
import com.qitmiaojie.pojo.User;
import com.qitmiaojie.service.MaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/Material")
public class MaterialController {

    @Autowired
    private MaterialService service;
    //添加物资


    @PutMapping("add")
    public Result AddMater(@RequestBody Material material){
        service.AddMaterial(material);
        return null;
    }
    //


    /**
     * 领取物资
     * @param uid 用户id
     * @param mid 物资id
     * @return 结果
     */
    @PostMapping("apply/{uid}/{mid}")
    public Result CallMyMater(@PathVariable(value = "uid") Integer uid,
                              @PathVariable(value = "mid") Integer mid){


        return service.AcquireMaterial(uid,mid);
    }

    @GetMapping("getAll")
    public Result getAllMater(){
        log.info("------获取物资信息中------");
        return Result.success("获取成功",service.list(
                new LambdaQueryWrapper<Material>()
                        .select(Material::getMid,Material::getMtitle,Material::getMdescription)


        ));
    }


    @GetMapping("SyncAny")
    public Result SyncAny(){

        log.info("------正在同步中------");
        boolean sync = service.SyncSqlAndRedis();
        if (sync){
            log.info("同步成功!");
        }else {
            log.error("同步失败!");
        }

        return sync  ? Result.success("同步成功",null):Result.error("同步失败");


    }
    @GetMapping("/myMaterials/{currentPage}/{pageSize}")
    public Result getById(@PathVariable("currentPage") Integer currentPage,@PathVariable("pageSize") Integer pageSize){
        return service.findByIdList(currentPage,pageSize);
    }



}
